package com.update
{
	import com.game.events.GameEventDispatcher;
	import com.game.events.GameEventType;
	import com.game.events.GameNotify;
	import com.net.interfaces.IDestroy;
	import com.update.interfaces.IUpdate;
	import com.utils.Config;

	public class Update extends GameNotify implements IUpdate
	{
		public static const PRIORITY_HIGH : int = 2;

		public static const PRIORITY_MIDDEN : int = 1;

		public static const PRIORITY_LOW : int = 0;

		/**
		 * 是否注册
		 */
		private var _isRegisterd : Boolean = false;

		/**
		 * 更新帧数
		 */
		private var _frameRate : uint = 0;

		/**
		 * 更新优先级
		 */
		protected var _priority : int;

		/**
		 * 更新间隔
		 */
		private var _frameInterval : uint = 0;

		/**
		 * 记录当前持续时间
		 */
		protected var frameElapsedTime : uint = 0;

		/**
		 * 是否需要检测更新
		 */
		private var checkUpdateable : Boolean = false;

		/**
		 * 是否销毁
		 */
		protected var _isDestroy : Boolean = false;

		protected var _registerdLevel : int;

		public function Update()
		{
			init();
		}

		/**
		 * 注册等级
		 */
		public function get registerdLevel() : int
		{
			return _registerdLevel;
		}

		/**
		 * 初始化
		 *
		 */
		protected function init() : void
		{

		}


		/**
		 * 如果是无限循环,责直接覆盖此函数
		 * 不是的话,责要设置生命周期
		 * @param delay
		 *
		 */
		public function update(delay : uint) : void
		{
		}

		public function checkUpdatable(elapsedTime : uint) : Boolean
		{
			if (!checkUpdateable)
			{
				return true;
			}
			frameElapsedTime += elapsedTime;

			if (frameElapsedTime >= _frameInterval)
			{
				frameElapsedTime -= _frameInterval;
				return true;
			}
			return false;
		}

		public function get frameRate() : uint
		{
			return _frameRate;
		}

		public function set frameRate(value : uint) : void
		{
			if (value <= 0 || value >= Config.frameRate)
			{
				_frameRate = Config.frameRate;
				checkUpdateable = false;
				return;
			}
			_frameRate = value;
			_frameInterval = 1000 / _frameRate;
			checkUpdateable = true;
		}

		public function get priority() : int
		{
			return _priority;
		}

		public function set priority(value : int) : void
		{
			_priority = value;
		}

		/**
		 *
		 * @param level   更新的等级,0低级,1中级,2高级
		 * @param priority优先级别
		 *
		 */
		public function registerd(registerdLevel : int = PRIORITY_LOW, priority : int = 0) : void
		{
			_priority = priority;
			_registerdLevel = registerdLevel;
			sendNotification(GameEventType.ADD_UPDATE, this);
		}

		public function unRegisterd() : void
		{
			sendNotification(GameEventType.DEL_UPDATE, this);
		}

		public function get isRegisterd() : Boolean
		{
			return _isRegisterd;
		}

		/**
		 * 更新间隔
		 */
		public function get frameInterval() : uint
		{
			return _frameInterval;
		}

		/**
		 * @private
		 */
		public function set frameInterval(value : uint) : void
		{
			_frameInterval = value;
			checkUpdateable = true;
		}

		/**
		 * 是否销毁
		 */
		override public function get isDestroy() : Boolean
		{
			return _isDestroy;
		}

		override public function destroy() : void
		{
			super.destroy();

			if (isRegisterd)
			{
				unRegisterd();
			}
			_isRegisterd = false;
			_isDestroy = true;
		}


	}
}